package user.application.service;

import user.domain.model.User;
import user.domain.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

@Service
public class UserService {

    private final UserRepository repo;

    @Autowired
    public UserService(UserRepository repo) {
        this.repo = repo;
    }

    public User createUser(User user) {
        return repo.save(user);
    }

    public void deleteUser(Long id) {
        repo.deleteById(id);
    }

    public Optional<User> getUser(Long id) {
        return repo.findById(id);
    }

    public Optional<User> getUserByUsername(String username) {
        return repo.findByUsername(username);
    }

    public List<User> listUsers() {
        return repo.findAll();
    }

    /*public User updateUser(Long id, User newUser) {
        return repo.findById(id).map(user -> {
            user.setUsername(newUser.getUsername());
            user.setPassword(newUser.getPassword());
            user.setEmail(newUser.getEmail());
            user.setRealName(newUser.getRealName());
            user.setRole(newUser.getRole());
            user.setAvatar(newUser.getAvatar());
            return repo.save(user);
        }).orElseThrow(() -> new RuntimeException("User not found"));
    } */

    public User updateUser(Long id, User newUser) {
        return repo.findById(id).map(user -> {

            if (StringUtils.hasText(newUser.getUsername())) {
                user.setUsername(newUser.getUsername());
            }

            if (StringUtils.hasText(newUser.getPassword())) {
                user.setPassword(newUser.getPassword());
            }

            if (StringUtils.hasText(newUser.getEmail())) {
                user.setEmail(newUser.getEmail());
            }

            if (StringUtils.hasText(newUser.getRealName())) {
                user.setRealName(newUser.getRealName());
            }

            if (StringUtils.hasText(newUser.getRole())) {
                user.setRole(newUser.getRole());
            }

            if (StringUtils.hasText(newUser.getAvatar())) {
                user.setAvatar(newUser.getAvatar());
            }

            return repo.save(user);
        }).orElseThrow(() -> new RuntimeException("User not found"));
    }

    // 注册逻辑（含重复校验）
    public User registerUser(User user) {
        if (repo.findByUsername(user.getUsername()).isPresent()) {
            throw new RuntimeException("Username already exists");
        }

        if (user.getEmail() != null && repo.findByEmail(user.getEmail()).isPresent()) {
            throw new RuntimeException("Email already exists");
        }

        return repo.save(user);
    }


    //登录逻辑
    public User login(String email, String password) {
        return repo.findByEmail(email)
                .filter(u -> u.getPassword().equals(password))
                .orElseThrow(() -> new RuntimeException("Invalid email address or password"));
    }


    // 修改密码逻辑
    public void resetPassword(String email, String newPassword) {
        User user = repo.findByEmail(email)
                .orElseThrow(() -> new RuntimeException("User not found"));
        user.setPassword(newPassword);
        repo.save(user);
    }
}
